<?php
/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */
namespace Admin\Controller;
use Think\Controller;
use Admin\Model\UpgradeModel;
class BaseController extends Controller {
    /**
     * 析构函数
     */
    function __construct() {
        parent::__construct();
        $upgradeModel = new UpgradeModel();
        //$upgradeMsg = $upgradeModel->checkVersion(); //升级包消息        
        $upgradeMsg = '';
        $this->assign('upgradeMsg', $upgradeMsg);
        $navigate_admin = navigate_admin();
        $this->assign('navigate_admin', $navigate_admin);
        //tpversion();
		//$this->checkOnePriv();
    }
    /*
     * 初始化操作
     */
    public function _initialize() {
        $this->assign('action', ACTION_NAME);
        //过滤不需要登陆的行为
        if (in_array(ACTION_NAME, array('login', 'logout', 'vertify')) || in_array(CONTROLLER_NAME, array('Ueditor', 'Uploadify'))) {
            //return;
        } else {
            if (session('admin_id') > 0) {
                $this->checkPriv(); //检查管理员菜单操作权限
            } else {
                $this->redirect('Admin/Admin/login');
                exit();
//                $this->error('请先登陆', U('Admin/Admin/login'), 1);
            }
        }
        $this->publicAssign();
    }
    /**
     * 保存公告变量到 smarty中 比如 导航 
     */
    public function publicAssign() {
        $xiarikui_config = array();
        $xrk_config = M('config')->select();
        foreach ($xrk_config as $k => $v) {
            $xiarikui_config[$v['inc_type'] . '_' . $v['name']] = $v['value'];
        }
        $this->assign('xiarikui_config', $xiarikui_config);
    }
    public function checkPriv() {
        $ctl = CONTROLLER_NAME;
        $act = ACTION_NAME;
        $act_list = session('act_list');
        //无需验证的操作
        $uneed_check = array('login', 'logout', 'vertifyHandle', 'vertify', 'imageUp', 'upload', 'loginTask');
        if ($ctl == 'Index' || $act_list == 'all') {
            //后台首页控制器无需验证,超级管理员无需验证
            return true;
        } elseif (strpos('ajax', $act) || in_array($act, $uneed_check)) {
            //所有ajax请求不需要验证权限
            return true;
        } else {
            $right = M('sys_menu')->where("id in ($act_list)")->getField('right', true);
            foreach ($right as $val) {
                $role_right .= $val . ',';
            }
            $role_right = explode(',', $role_right);
            //检查是否拥有此操作权限
            if (!in_array($ctl . 'Controller@' . $act, $role_right)) {
                $this->error('您没有操作权限,请联系超级管理员分配权限', U('Admin/Index/welcome'));
            }
        }
    }
	protected $allCate = array();
	function getCate($model_style,$pid=0,$level=0,$tablename,$map='cat_id=', $sort ,$fields = null, $limit = null){
		$model = D($model_style);
		$where = $map.$pid;
		$data= $model->where($where)->order($sort)->field($fields)->limit($limit)->select(); 
		if($data){	
			foreach($data as $v){
				if($v['id'] == $this->cateid && !empty($v['id'])){
					continue;
				}
				$v['level'] = $level+1;
				$this->allCate[] = $v;
				self::getCate($model_style,$v['id'] , $v['level'] , $tablename , $map , $sort ,$fields, $limit);
			}
		}
		return $this->allCate;
	}

    function _uploadOne($name,$id,$diff=true) {
        if($id && $diff){
            if(strtolower(CONTROLLER_NAME)=='user'){
                $img = M('users')->where(array('id'=>$id))->getField($name);
                if($img){
                    unlink('./Public/upload/'.strtolower(CONTROLLER_NAME).'/'.$img);
                }
            }
        }
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;
        $upload->rootPath = './Public/upload/';
        $upload->savePath = strtolower(CONTROLLER_NAME).'/';
        $upload->saveName = array('uniqid','');
        $upload->exts     = array('jpg','gif','png','jpeg');
        $upload->autoSub  = false;//子目录
        $upload->subName  = array('Public','Ymd');
        $info   =   $upload->uploadOne($_FILES[$name]);
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功 获取上传文件信息
            $image = new \Think\Image();
            $image->open('./Public/upload/'.strtolower(CONTROLLER_NAME).'/'.$info['savename']);
            $image->thumb(1024,1024)->save('./Public/upload/'.strtolower(CONTROLLER_NAME).'/s_'.$info['savename']);
            unlink('./Public/upload/'.strtolower(CONTROLLER_NAME).'/'.$info['savename']);
            return 's_'.$info['savename'];
        }
    }
}
